// i.r(t), i.d(t, { default: () => PreRendererModule })

import Module from "./Module"
import SymbolList from "../SymbolList"
import SweepData from "../data/SweepData"
import { RestrictedSweepsClearedMessge, RestrictedSweepsSetMessge } from "../message/sweeps.messge"

import funcUtil from "../utils/func.util"
var n = {
  0: "None",
  1: "Queued",
  2: "Rendering",
  3: "Rendered",
  None: 0,
  Queued: 1,
  Rendered: 3,
  Rendering: 2
}

class PreRenderer {
  statusMap: any
  active: any[]
  queued: any[]
  panoRenderer: any
  enabled: boolean
  constructor(e) {
    ;(this.statusMap = {}), (this.active = []), (this.queued = []), (this.panoRenderer = e), this.disablePreRendering()
  }
  init() {}
  dispose() {}
  activate(e) {}
  deactivate(e) {
    this.disablePreRendering()
  }
  enablePreRendering() {
    this.enabled = !0
  }
  disablePreRendering() {
    this.enabled = !1
  }
  render() {}
  beforeRender() {
    this.enabled && this.processQueued()
  }
  tryPreRender(e) {
    return this.getPreRenderState(e) === n.None && (this.queued.push(e), (this.statusMap[e] = n.Queued), !0)
  }
  getPreRenderState(e) {
    const t = this.statusMap[e]
    return void 0 !== t ? t : n.None
  }
  processQueued() {
    let e = 0
    for (const t of Object.keys(this.statusMap)) this.statusMap[t] === n.Rendering && e++
    if (0 === e && this.queued.length > 0) {
      const e = this.queued.shift()
      if (e) {
        this.active.push(e), (this.statusMap[e] = n.Rendering)
        this.panoRenderer.activateSweep(e).then(() => {
          this.onRendered(e)
        })
      }
    }
  }
  cleanup(e = []) {
    const t = funcUtil.valToFalse(e),
      i = []
    for (const e of this.queued) t[e] ? i.push(e) : (this.statusMap[e] = n.None)
    ;(this.queued.length = 0), this.queued.push.apply(this.queued, i)
    const s = []
    for (const e of this.active) t[e] ? s.push(e) : (this.statusMap[e] = n.None)
    ;(this.active.length = 0), this.active.push.apply(this.active, s)
  }
  onRendered(e) {
    this.statusMap[e] = n.Rendered
  }
}
const d = Object.freeze({
  prerender: { preRenderTourPanos: funcUtil.getUrlParams("pre", 1) }
})
export default class PreRendererModule extends Module {
  lastPrerendered: any
  sweepData: any
  panoRenderer: any
  preRenderer: PreRenderer
  currentRestrictedSweeps: any
  constructor() {
    super(...arguments), (this.name = "prerenderer-module"), (this.lastPrerendered = null)
  }
  async init(e, t) {
    if (d.prerender.preRenderTourPanos || e.preRenderTourPanos) {
      this.sweepData = await t.market.waitForData(SweepData)
      const e = await t.getModuleBySymbol(SymbolList.SWEEP_PANO)
      ;(this.panoRenderer = e.getRenderer()),
        (this.preRenderer = new PreRenderer(this.panoRenderer)),
        t.addComponent(this, this.preRenderer),
        this.bindings.push(
          t.subscribe(RestrictedSweepsSetMessge, e => {
            this.preRenderer.enablePreRendering(),
              (this.lastPrerendered = null),
              e.sweepIds && e.sweepIds.length >= 3 && ((this.lastPrerendered = e.sweepIds[2]), this.preRenderer.tryPreRender(this.lastPrerendered)),
              this.cleanup(this.sweepData),
              (this.currentRestrictedSweeps = e.sweepIds)
          })
        ),
        this.bindings.push(
          t.subscribe(RestrictedSweepsClearedMessge, e => {
            this.preRenderer.disablePreRendering(), this.cleanup(this.sweepData, !0), (this.currentRestrictedSweeps = null)
          })
        )
    }
  }
  getCurrentSweeps() {
    return this.currentRestrictedSweeps || []
  }
  cleanup(e, t = !1) {
    const i = []
    e.transition.active
      ? (e.transition.from && i.push(e.transition.from), e.transition.to && i.push(e.transition.to))
      : e.currentSweep && i.push(e.currentSweep),
      this.lastPrerendered && i.push(this.lastPrerendered),
      t && this.panoRenderer.freeAllTextures(i),
      this.preRenderer.cleanup(i)
  }
}
